package org.easy.bytetts;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.easy.bytetts.dto.ByteTtsVoiceEnums;
import org.easy.core.util.HttpUtil;
import org.easy.core.util.UUID;
import org.easy.bytetts.dto.ByteTtsResponse;

import java.util.HashMap;
import java.util.Map;

public class ByteTtsHttpClient {
    private String accessToken;
    private String appid;

    public ByteTtsHttpClient(String appid, String accessToken) {
        this.appid = appid;
        this.accessToken = accessToken;
    }

    private static final String API_URL = "https://openspeech.bytedance.com/api/v1/tts";


    /***
     * 文字转语音
     * @param userId
     * @param voiceType
     * @param text
     * @return mp3格式
     */
    public ByteTtsResponse tts(Long userId, ByteTtsVoiceEnums voiceType, String text) {

        JSONObject app = new JSONObject();
        app.put("appid", appid);
        app.put("token", "access_token");
        app.put("cluster", "volcano_tts");

        JSONObject user = new JSONObject();
        user.put("uid", userId);

        JSONObject audio = new JSONObject();
        audio.put("voice_type", voiceType.getVoiceType());
        audio.put("encoding", "pcm ");
        audio.put("compression_rate", 1);
        audio.put("rate", 24000);


        //        speed_ratio	语速	2	float		[0.2,3]，默认为1，通常保留一位小数即可
        audio.put("speed_ratio", 1.0);
        //volume_ratio	音量	2	float		0.1, 3]，默认为1，通常保留一位小数即可
        audio.put("volume_ratio", 1.0);
//        pitch_ratio	音高	2	float		[0.1, 3]，默认为1，通常保留一位小数即可
        audio.put("pitch_ratio", 1.0);
        audio.put("language", "cn");
//        audio.put("emotion", "scare");

        JSONObject request = new JSONObject();
        request.put("reqid", UUID.fastUUID().toString());
        request.put("text", text);
        request.put("text_type", "plain");
        request.put("operation", "query");
        request.put("with_frontend", 1);
        request.put("frontend_type", "unitTson");

        JSONObject json = new JSONObject();
        json.put("app", app);
        json.put("user", user);
        json.put("audio", audio);
        json.put("request", request);

        String post = post(API_URL, json);

        return JSON.parseObject(post, ByteTtsResponse.class);
    }
    public ByteTtsResponse tts(Long userId, ByteTtsVoiceEnums voiceType, String text,Float speedRatio, Float pitchRatio ) {

        JSONObject app = new JSONObject();
        app.put("appid", appid);
        app.put("token", "access_token");
        app.put("cluster", "volcano_tts");

        JSONObject user = new JSONObject();
        user.put("uid", userId);

        JSONObject audio = new JSONObject();
        audio.put("voice_type", voiceType.getVoiceType());
        audio.put("encoding", "mp3");
        audio.put("compression_rate", 1);
        audio.put("rate", 24000);
        if(speedRatio == null) {
            audio.put("speed_ratio", 1.0);
        }else {
            audio.put("speed_ratio", speedRatio);
        }
        audio.put("volume_ratio", 1.0);

        if(pitchRatio == null) {
            audio.put("pitch_ratio", 1.0);
        }else {
            audio.put("pitch_ratio", pitchRatio);
        }
        audio.put("language", "cn");
//        audio.put("emotion", "scare");

        JSONObject request = new JSONObject();
        request.put("reqid", UUID.fastUUID().toString());
        request.put("text", text);
        request.put("text_type", "plain");
        request.put("operation", "query");
        request.put("with_frontend", 1);
        request.put("frontend_type", "unitTson");

        JSONObject json = new JSONObject();
        json.put("app", app);
        json.put("user", user);
        json.put("audio", audio);
        json.put("request", request);

        String post = post(API_URL, json);

        return JSON.parseObject(post, ByteTtsResponse.class);
    }
    public String post(String url, Object json) {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Bearer; " + accessToken);
        return HttpUtil.postJson(url, header, json);
    }


}
